﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Admin.Home.Render.Template;
using System.Collections;
using System.Reflection;
using Portal.Core;

namespace Portal.Web.Utility
{
    public class AdminHomeRenderUtility
    {
        private static IAdminHomeRender[] LoadAssembly(string filename)
        {
            ArrayList ArrayListModules = new ArrayList();
            Assembly a = Assembly.LoadFrom(filename);
            if (a == null) throw new Exception("Assembly " + filename + " not found or not valid");
            foreach (Module mod in a.GetLoadedModules())
            {
                foreach (Type ty in mod.GetTypes())
                {
                    foreach (Type intf in ty.GetInterfaces())
                    {
                        if (intf == typeof(IAdminHomeRender))
                        {
                            IAdminHomeRender w = (IAdminHomeRender)a.CreateInstance(ty.FullName);
                            ArrayListModules.Add(w);
                        }
                    }
                }
            }
            IAdminHomeRender[] ws = new IAdminHomeRender[ArrayListModules.Count];
            for (int iIndex = 0; iIndex < ArrayListModules.Count; iIndex++)
            {
                ws[iIndex] = (IAdminHomeRender)ArrayListModules[iIndex];
            }
            return ws;
        }


        public static IAdminHomeRender[] LoadPlugIn(string SiteId)
        {
            SiteParam oSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string Path = oSiteParam.DllPath;
            string[] Files = System.IO.Directory.GetFiles(Path, "*.dll");
            ArrayList ArrayListServives = new ArrayList();
            for (int iIndex = 0; iIndex < Files.Length; iIndex++)
            {
                try
                {
                    string FileDll = Files[iIndex];
                    IAdminHomeRender[]
                        asms = LoadAssembly(FileDll);
                    for (int jIndex = 0; jIndex < asms.Length; jIndex++)
                    {
                        if (asms[jIndex].ServiceId != null)
                        {
                            ArrayListServives.Add(asms[jIndex]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.Source = "";
                }
            }
            IAdminHomeRender[] WebParts = new IAdminHomeRender[ArrayListServives.Count];
            for (int iIndex = 0; iIndex < ArrayListServives.Count; iIndex++)
            {
                WebParts[iIndex] = (IAdminHomeRender)ArrayListServives[iIndex];
            }

            return WebParts;
        }


        public static IAdminHomeRender Find(string SiteId, string ServiceId)
        {
            IAdminHomeRender[] found = LoadPlugIn(SiteId);
            for (int iIndex = 0; iIndex < found.Length; iIndex++)
            {
                if (found[iIndex].ServiceId != null)
                {
                    if (found[iIndex].ServiceId.Trim().ToLower().Equals(ServiceId.Trim().ToLower()))
                    {
                        return found[iIndex];
                    }
                }
            }
            return null;
        }
    }
}
